package com.wilmer.stemCourseCon.controller;

import com.wilmer.common.annotation.Log;
import com.wilmer.common.constant.UserConstants;
import com.wilmer.common.core.controller.BaseController;
import com.wilmer.common.core.domain.AjaxResult;
import com.wilmer.common.core.domain.entity.SysDept;
import com.wilmer.common.enums.BusinessType;
import com.wilmer.common.utils.StringUtils;
import com.wilmer.stemCourseCon.domain.StemCourseCon;
import com.wilmer.stemCourseCon.service.StemCourseConService;
import com.wilmer.system.service.ISysDeptService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/stemCourseCon")
public class YwStemCourseConController extends BaseController {
    @Autowired
    private StemCourseConService stemCourseConService;

    /**
     * 获取课程组织列表列表
     */
    @PreAuthorize("@ss.hasPermi('yw:stemCourseCon:list')")
    @GetMapping("/list")
    public AjaxResult list(StemCourseCon stemCourseCon) {
        List<StemCourseCon> stemCourseCons = stemCourseConService.selectStemCourseConList(stemCourseCon);
        return success(stemCourseCons);
    }

    /**
     * 查询课程结构列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('yw:stemCourseCon:list')")
    @GetMapping("/list/exclude/{stemCourseConId}")
    public AjaxResult excludeChild(@PathVariable(value = "stemCourseConId" , required = false) Long stemCourseConId) {
        List<StemCourseCon> stemCourseCons = stemCourseConService.selectStemCourseConList(new StemCourseCon());
        stemCourseCons.removeIf(d -> d.getConId().intValue() == stemCourseConId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), stemCourseConId + ""));
        return success(stemCourseCons);
    }

    /**
     * 根据课程结构编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('yw:stemCourseCon:query')")
    @GetMapping(value = "/{stemCourseConId}")
    public AjaxResult getInfo(@PathVariable Long stemCourseConId) {
        stemCourseConService.checkStemCourseConDataScope(stemCourseConId);
        return success(stemCourseConService.selectStemCourseConById(stemCourseConId));
    }

    /**
     * 新增课程结构
     */
    @PreAuthorize("@ss.hasPermi('yw:stemCourseCon:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StemCourseCon stemCourseCon) {
        if (!stemCourseConService.checkStemCourseConNameUnique(stemCourseCon)) {
            return error("新增课程结构'" + stemCourseCon.getConName() + "'失败，课程结构名称已存在");
        }
        stemCourseCon.setCreateBy(getUsername());
        return toAjax(stemCourseConService.insertStemCourseCon(stemCourseCon));
    }

    /**
     * 修改课程结构
     */
    @PreAuthorize("@ss.hasPermi('yw:stemCourseCon:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StemCourseCon stemCourseCon) {
        Long stemCourseConId = stemCourseCon.getConId();
        stemCourseConService.checkStemCourseConDataScope(stemCourseConId);
        if (!stemCourseConService.checkStemCourseConNameUnique(stemCourseCon)) {
            return error("修改课程结构'" + stemCourseCon.getConName() + "'失败，课程结构名称已存在");
        } else if (stemCourseCon.getParentId().equals(stemCourseConId)) {
            return error("修改课程结构'" + stemCourseCon.getConName() + "'失败，上级课程结构不能是自己");
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, stemCourseCon.getStatus()) && stemCourseConService.selectNormalChildrenStemCourseConById(stemCourseConId) > 0) {
            return error("该课程结构包含未停用的子课程结构！");
        }
        stemCourseCon.setUpdateBy(getUsername());
        return toAjax(stemCourseConService.updateStemCourseCon(stemCourseCon));
    }

    /**
     * 删除课程结构
     */
    @PreAuthorize("@ss.hasPermi('yw:stemCourseCon:remove')")
    @DeleteMapping("/{stemCourseConId}")
    public AjaxResult remove(@PathVariable Long stemCourseConId) {
        if (stemCourseConService.hasChildByStemCourseConId(stemCourseConId)) {
            return warn("存在下级课程结构,不允许删除");
        }
        if (stemCourseConService.checkStemCourseConExistUser(stemCourseConId)) {
            return warn("课程结构存在课程,不允许删除");
        }
        stemCourseConService.checkStemCourseConDataScope(stemCourseConId);
        return toAjax(stemCourseConService.deleteStemCourseConById(stemCourseConId));
    }
}
